<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\CommonModel;
use app\admin\model\HomeModel;
use app\admin\model\TalkModel;
use app\admin\model\UserModel;
use cmf\controller\AdminBaseController;
use think\Db;

//首页推荐
class HomeController extends AdminBaseController
{
    public function index(){
        $param = $this->request->param();
        $type = UserModel::EXPERT_TYPE;
        $gains = HomeModel::GAINS;
        //查询球队表
        $arr_team = $this->getTeam();
        $list = Db::name('home')
            ->where(function($query) use($param){
                if(isset($param['type_id']) && $param['type_id'] != '-1'){
                    $query->where('type',$param['type_id']);
                }
            })
            ->field('content,update_time,video',true)
            ->order('id desc')
            ->paginate(null,false,['query'=>$param]);
        $page = $list->render();

        $bat_ids = array_unique(array_column($list->items(),'bat_id'));
        $commonModel = new CommonModel();
        $res_bat = $commonModel->selectWhereData('bat_match',['id'=>$bat_ids],'id,team_main_id,team_pass_id,match_time');
        $data = $list->items();
        foreach($data as &$value){
            $value['type'] = $type[$value['type']];
            $value['gains'] = $gains[$value['gains']];
            $value['bat_name'] = '';
            foreach ($res_bat as $b_value){
                if($value['bat_id'] == $b_value['id']){
                    $team_main_name = $arr_team[$b_value['team_main_id']]??'';
                    $team_pass_name = $arr_team[$b_value['team_pass_id']]??'';
                    $match_time = date('Y-m-d H:i:s',$b_value['match_time']);
                    $value['bat_name'] = $team_main_name.'&nbsp;VS&nbsp;'.$team_pass_name.'&nbsp;&nbsp;（'.$match_time.'）';
                    break;
                }
            }
        }
        $this->assign('list', $data);
        $this->assign('page', $page);
        //类型
        $this->assign('type',UserModel::EXPERT_TYPE);
        //搜索回显
        $this->assign('type_id',$param['type_id']??-1);
        return $this->fetch();
    }

    //添加页面
    public function add(){
        //类型
        $this->assign('type',UserModel::EXPERT_TYPE);
        //战况
        $this->assign('gains',HomeModel::GAINS);
        return $this->fetch();
    }

    //添加提交
    public function addPost(){
        $data = $this->request->param();
        $result = $this->validate($data,'Home.add');
        if($result !== true){
            $this->error($result);
        }

        $commonModel = new CommonModel();
        $data['content'] = $commonModel->setPostContentAttr($data['content']);
        $homeModel = new HomeModel();
        $res = $homeModel->allowField(true)->create($data);
        if($res){
            $this->success('添加成功！', url('Home/index'));
        }else{
            $this->error('添加失败！');
        }
    }

    //编辑页面
    public function edit(){
        $id = $this->request->param('id');
        $post = Db::name('home')
            ->where(['id'=>$id])
            ->find();
        $commonModel = new CommonModel();
        $post['content'] = $commonModel->getPostContentAttr($post['content']);
        $this->assign('post',$post);
        //类型
        $this->assign('type',UserModel::EXPERT_TYPE);
        //战况
        $this->assign('gains',HomeModel::GAINS);
        return $this->fetch();
    }

    //编辑提交
    public function editPost(){
        $data = $this->request->param();
        $result = $this->validate($data,'Home.edit');
        if($result !== true){
            $this->error($result);
        }

        $commonModel = new CommonModel();
        $data['content'] = $commonModel->setPostContentAttr($data['content']);
        $homeModel = new HomeModel();
        $res = $homeModel->allowField(true)->where(['id'=>$data['id']])->update($data);
        if($res){
            $this->success('保存成功！', url('Home/index'));
        }else{
            $this->error('保存失败！');
        }
    }

    //删除
    public function delete(){
        $id = $this->request->param('id');
        $homeModel = new HomeModel();
        $res = $homeModel->where('id',$id)->delete();
        if($res){
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }

    //查询对战VS，付费聊吧
    public function getBatMatch(){
        $type_id = $this->request->param('type_id');
        $arr['bat'] = [];
        $arr['talk'] = [];
        if($type_id == ''){
            echo json_encode($arr);exit;
        }
        //查询联赛表
        $commonModel = new CommonModel();
        //查询对战表
        $res_bat = $commonModel->selectWhereData('bat_match',['type'=>$type_id,'status'=>HomeModel::BAT_IN_STATUS],'id,team_main_id,team_pass_id,match_time');
        //查询球队表
        $arr_team = $this->getTeam();
        $res = [];
        foreach($res_bat as $b_key=>$b_value){
            $res[$b_key]['bat_id'] = $b_value['id'];
            $team_main_name = $arr_team[$b_value['team_main_id']]??'';
            $team_pass_name = $arr_team[$b_value['team_pass_id']]??'';
            $match_time = date('Y-m-d H:i:s',$b_value['match_time']);
            $res[$b_key]['bat_name'] = $team_main_name.'&nbsp;VS&nbsp;'.$team_pass_name.'&nbsp;&nbsp;（'.$match_time.'）';
        }
        if($res){
            $arr['bat'] = $res;
        }

        //付费聊吧
        $commonModel = new CommonModel();
        $where[] = ['type','=',$type_id];
        $where[] = ['type1','<>',TalkModel::TALK_FREE];
        $res_talk = $commonModel->selectWhereData('talk',$where,'id,title');
        if($res_talk){
            $arr['talk'] = $res_talk;
        }
        echo json_encode($arr);exit;
    }

    //查询球队表
    public function getTeam(){
        $commonModel = new CommonModel();
        $res = $commonModel->selectData('team','id,team_name');
        $res_team = [];
        foreach($res as $value){
            $res_team[$value['id']] = $value['team_name'];
        }
        return $res_team;
    }

    //查询付费聊吧表
    public function getTalk(){
        $type = $this->request->param('type');
        $commonModel = new CommonModel();
        $where[] = ['type','=',$type];
        $where[] = ['type1','<>',TalkModel::TALK_FREE];
        $res = $commonModel->selectWhereData('talk',$where,'id,title');
        $res_talk = [];
        foreach($res as $value){
            $res_talk[$value['id']] = $value['title'];
        }
        return $res_talk;
    }
}